package com.bahiaapk;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bahiaapk.customize.MediaListAdapter;
import com.bahiaapk.customize.PullToRefreshListView;
import com.bahiaapk.customize.PullToRefreshListView.OnRefreshListener;
import com.bahiaapk.dto.Media;

/**
 * @author Igor Faria
 *
 */
public class MediaActivity extends ListActivity {
	
	//private WebView webView;
	private Button videosBTN;
	private Button popularMediaBTN;
	private Button podcastsBTN;
	private TextView videosTV;
	private TextView popularMediaTV;
	private TextView podcastsTV;

	private List<Media> list;
	private ListAdapter adapterListMedia;

	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.media);
    	
    	
// 		startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));


        
        videosBTN = (Button)findViewById(R.id.videosBTN);
        popularMediaBTN = (Button)findViewById(R.id.popularMediaBTN);
        podcastsBTN = (Button)findViewById(R.id.podcastsBTN);
        videosTV = (TextView)findViewById(R.id.videosTV);
        popularMediaTV = (TextView)findViewById(R.id.popularMediaTV);
        podcastsTV = (TextView)findViewById(R.id.podcastsTV);
        
        listAllVideo();
        // Set a listener to be invoked when the list should be refreshed.
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });

    }
    
    
    private void listAllVideo() {
    	list = new ArrayList<Media>();
    	
    	Media media = null;
    	for (int i = 0; i < 12; i++) {
    		media = new Media();
    		media.setTitle("Bahia 0 x 2 Palmeiras, melhores momentos");
    		media.setDate(new Date());
    		media.setUrlImage("http://i2.ytimg.com/vi/Uhst9VhqyX8/default.jpg");
    		media.setTime("03:45");

    		list.add(media);
		}
    	
    	adapterListMedia = (ListAdapter) new MediaListAdapter(this, list);        
        
	    setListAdapter(adapterListMedia);
	}
    

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                ;
            }
			return null;
           
        }

        @Override
        protected void onPostExecute(String[] result) {
        
            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) getListView()).onRefreshComplete();

            super.onPostExecute(result);
        }
    }
    
    public void changeNews(View view){
		
    	videosBTN.setBackgroundResource(Color.TRANSPARENT);
    	videosTV.setTextColor(Color.BLACK);
		
		popularMediaBTN.setBackgroundResource(Color.TRANSPARENT);
		popularMediaTV.setTextColor(Color.BLACK);
		
		podcastsBTN.setBackgroundResource(Color.TRANSPARENT);
		podcastsTV.setTextColor(Color.BLACK);
		
		switch (view.getId()) {
            case R.id.videosBTN: {
            	videosBTN.setBackgroundResource(R.drawable.img_btn_news2);
        		videosTV.setTextColor(Color.WHITE);
                break;
            }
                
            case R.id.popularMediaBTN: {
            	popularMediaBTN.setBackgroundResource(R.drawable.img_btn_news2);
            	popularMediaTV.setTextColor(Color.WHITE);
            
                break;
            }
            
            case R.id.podcastsBTN: {
            	podcastsBTN.setBackgroundResource(R.drawable.img_btn_news2);
            	podcastsTV.setTextColor(Color.WHITE);
            	
                break;
            }
            
           
		}
	}

    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) { 
		super.onListItemClick(l, v, position, id);
//		   Media newsSelectedFromList = (Media) (adapterListMedia.getItem(position-1));
//	 		startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(newsSelectedFromList.getUrlMedia())));
		
		Intent	intent = new Intent(this, MediaPlayerActivity.class);
		startActivity(intent);
	       return;
	}
}
